package com.ling.demo

import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.ling.common.library.media.impl.LocalMediaFinderImp
import com.ling.common.library.media.model.AlbumType
import com.ling.common.library.permission.PermissionUtil
import com.ling.common.library.permission.model.MediaType
import com.ling.common.library.utils.MkvUtil
import com.ling.common.library.utils.TouchClickUtil
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity(){
    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MkvUtil.initialize(this)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.video).setOnClickListener {
            if(PermissionUtil.hasMediaPermission(this , MediaType.VIDEO)){
                lifecycleScope.launch {
                    LocalMediaFinderImp.getSingleInstance().getMediaByPage(context = this@MainActivity , pageIndex = 1 , albumType = AlbumType.VIDEO)?.let {
                        println("--->" + it.data.size+ "  "+ it)
                    }

                    LocalMediaFinderImp.getSingleInstance().getMedia(context = this@MainActivity , albumType =  AlbumType.VIDEO)?.let {
                        println("--->" + it.size  + "  "+ it)
                    }

                    LocalMediaFinderImp.getSingleInstance().getAlbum(context = this@MainActivity , albumType = AlbumType.VIDEO)?.let {
                        println("--->" + it.size  + "  "+ it)
                    }
                }
            }else{
                requestPermissions(arrayOf(PermissionUtil.getMediaPermission(context = this , mediaType = MediaType.VIDEO)) , 1001)
            }
        }
        TouchClickUtil.getNewInstance(findViewById(R.id.image))
            .setParentView(findViewById(R.id.main))
            .setParentDefaultBg(R.color.white)
            .setParentTouchingBg(R.color.black)
            .setTouchViewClickListener{
                if(PermissionUtil.hasMediaPermission(context = this , mediaType = MediaType.IMAGE)){
                    lifecycleScope.launch {
                        LocalMediaFinderImp.getSingleInstance().getMediaByPage(this@MainActivity , 1 , AlbumType.IMAGE)?.let {
                            println("--->" + it.data.size+ "  "+ it.hasMore)
                        }
                    }
                }else{
                    requestPermissions(arrayOf(PermissionUtil.getMediaPermission(this , MediaType.IMAGE)) , 1001)
                }
            }
            .setTouchViewLongClickListener {
                if(PermissionUtil.hasMediaPermission(context = this , mediaType = MediaType.IMAGE)){
                    lifecycleScope.launch {
                        LocalMediaFinderImp.getSingleInstance().getMedia(this@MainActivity ,  AlbumType.IMAGE)?.let {
                            println("--->" + it.size+ "  "+ it)
                        }
                    }
                }else{
                    requestPermissions(arrayOf(PermissionUtil.getMediaPermission(this , MediaType.IMAGE)) , 1001)
                }
            }
        findViewById<Button>(R.id.audio).setOnClickListener {
            if(PermissionUtil.hasMediaPermission(this , MediaType.AUDIO)){
                lifecycleScope.launch {
                    LocalMediaFinderImp.getSingleInstance().getMediaByPage(this@MainActivity , 1 , AlbumType.AUDIO)?.let {
                        println("--->" + it.data.size+ "  "+ it)
                    }

                    LocalMediaFinderImp.getSingleInstance().getMedia(this@MainActivity , AlbumType.AUDIO)?.let {
                        println("--->" + it.size  + "  "+ it)
                    }

                    LocalMediaFinderImp.getSingleInstance().getAlbum(context = this@MainActivity , albumType = AlbumType.AUDIO)?.let {
                        println("--->" + it.size  + "  "+ it)
                    }
                }
            }else{
                requestPermissions(arrayOf(PermissionUtil.getMediaPermission(this , MediaType.AUDIO)) , 1001)
            }
        }

        findViewById<Button>(R.id.video_image).setOnClickListener {
            if(PermissionUtil.hasMediaPermission(this , MediaType.VIDEO) && PermissionUtil.hasMediaPermission(this , MediaType.IMAGE)){
                lifecycleScope.launch {
                    LocalMediaFinderImp.getSingleInstance().getMediaByPage(this@MainActivity , 1 , AlbumType.VIDEO_IMAGE)?.let {
                        println("--->" + it.data.size+ "  "+ it)
                    }

                    LocalMediaFinderImp.getSingleInstance().getMedia(this@MainActivity , AlbumType.VIDEO_IMAGE)?.let {
                        println("--->" + it.size  + "  "+ it)
                    }

                    LocalMediaFinderImp.getSingleInstance().getAlbum(context = this@MainActivity , fillAlbumItem = true , albumType =  AlbumType.VIDEO_IMAGE)?.let {
                        println("--->" + it.size  + "  "+ it)
                    }
                }
            }else{
                requestPermissions(arrayOf(PermissionUtil.getMediaPermission(this , MediaType.VIDEO) , PermissionUtil.getMediaPermission(this , MediaType.IMAGE)) , 1001)
            }
        }

        TouchClickUtil.getNewInstance(findViewById<Button>(R.id.mmkv_test))
            .setTouchViewClickListener {
                println("----->${MkvUtil.setString("test" , "你好")}")
            }
            .setTouchViewLongClickListener {
                if(!it){
                    println("----->${MkvUtil.getString(name = "test" , defaultValue = "不好")}")
                }
            }
    }
}